home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / OGRID110 / TCUTIL.PAS < prev    next >
Pascal/Delphi Source File  |  1995-06-01  |  9KB  |  380 lines

  1.  
  2. { Copyright (c) 1989,90 by Borland International, Inc. }
  3.  
  4. unit TCUtil;
  5. { Turbo Pascal 6.0 object-oriented example miscellaneous utility routines.
  6.   This unit is used by TCALC.PAS.
  7.   See TCALC.DOC for an more information about this example.
  8. }
  9.  
  10. {$S-}
  11.  
  12. interface
  13.  
  14. uses Crt, Dos;
  15.  
  16. const
  17.   FreeListItems = 100;      { Sets the size of the free list }
  18.   Letters : set of Char = ['A'..'Z', 'a'..'z'];
  19.   Numbers : set of Char = ['0'..'9'];
  20.   ErrAbstractCall = 'Call to abstract method ';
  21.   ErrNoMemory = 'Out of memory';
  22.   NULL = 0;
  23.   BS = 8;
  24.   FF = 12;
  25.   CR = 13;
  26.   ESC = 27;
  27.   F1 = 15104;
  28.   F2 = 15360;
  29.   F3 = 15616;
  30.   F4 = 15872;
  31.   F5 = 16128;
  32.   F6 = 16384;
  33.   F7 = 16640;
  34.   F8 = 16896;
  35.   F9 = 17152;
  36.   F10 = 17408;
  37.   AltF1 = 26624;
  38.   AltF2 = 26880;
  39.   AltF3 = 27136;
  40.   AltF4 = 27392;
  41.   AltF5 = 27648;
  42.   AltF6 = 27904;
  43.   AltF7 = 28160;
  44.   AltF8 = 28416;
  45.   AltF9 = 28672;
  46.   AltF10 = 28928;
  47.   HomeKey = 18176;
  48.   UpKey = 18432;
  49.   PgUpKey = 18688;
  50.   LeftKey = 19200;
  51.   RightKey = 19712;
  52.   EndKey = 20224;
  53.   DownKey = 20480;
  54.   PgDnKey = 20736;
  55.   InsKey = 20992;
  56.   DelKey = 21248;
  57.   CtrlLeftKey = 29440;
  58.   CtrlRightKey = 29696;
  59.   AltX = 11520;
  60.  
  61. type
  62.   ProcPtr = procedure;
  63.   StringPtr = ^String;
  64.   WordPtr = ^Word;
  65.   CharSet = set of Char;
  66.  
  67. procedure Abstract(Name : String);
  68.  
  69. function Compare(var P1, P2; Length : Word) : Boolean;
  70.  
  71. function GetKey : Word;
  72.  
  73. function GetKeyUpCase : Word;
  74.  
  75. function GetKeyChar(Legal : CharSet) : Char;
  76.  
  77. procedure Abort(Message : String);
  78.  
  79. procedure Beep;
  80.  
  81. function FileExists(F : String) : Boolean;
  82.  
  83. function Min(N1, N2 : Longint) : Longint;
  84.  
  85. function Max(N1, N2 : Longint) : Longint;
  86.  
  87. function NumToString(N : Longint) : String;
  88.  
  89. function UpperCase(S : String) : String;
  90.  
  91. function FillString(Len : Byte; Ch : Char) : String;
  92.  
  93. function TruncStr(TString : String; Len : Byte) : String;
  94.  
  95. function PadChar(PString : String; Ch : Char; Len : Byte) : String;
  96.  
  97. function CenterStr(S : String; Width : Byte) : String;
  98.  
  99. function LeftJustStr(S : String; Width : Byte) : String;
  100.  
  101. function RightJustStr(S : String; Width : Byte) : String;
  102.  
  103. function ColToString(Col : Word) : String;
  104.  
  105. function RowToString(Row : Word) : String;
  106.  
  107. function StringToCol(S : String; MaxCols : Word) : Word;
  108.  
  109. function StringToRow(S : String; MaxRows : Word) : Word;
  110.  
  111. procedure ClearInputBuffer;
  112.  
  113. implementation
  114.  
  115. const
  116.   AbortMessage : String[80] = '';
  117.  
  118. var
  119.   SavedExitProc : Pointer;
  120.  
  121. procedure Abstract(Name : String);
  122. { Called by abstract methods which should never be executed.  Aborts the
  123.   program with an error message.
  124. }
  125. begin
  126.   Abort(ErrAbstractCall + Name);
  127. end; { Abstract }
  128.  
  129. {$L TCCOMPAR}
  130.  
  131. function Compare(var P1, P2; Length : Word) : Boolean; external;
  132. { Compares two areas of memory - see TCCOMPAR.ASM for the source }
  133.  
  134. function GetKey : Word;
  135. { Returns the value of a key that was pressed - handles extended characters
  136.   (function keys, etc.) by treating all characters as words.
  137. }
  138. var
  139.   Ch : Char;
  140. begin
  141.   Ch := ReadKey;
  142.   if Ord(Ch) = NULL then                { Extended character }
  143.     GetKey := Word(Ord(ReadKey)) shl 8
  144.   else
  145.     GetKey := Ord(Ch);                  { Normal character }
  146. end; { GetKey }
  147.  
  148. function GetKeyUpCase : Word;
  149. { Returns the upper case equivalent of a character from the keyboard }
  150. var
  151.   Ch : Word;
  152. begin
  153.   Ch := GetKey;
  154.   if (Ch >= Ord(' ')) and (Ch <= Ord('~')) then
  155.     GetKeyUpCase := Ord(UpCase(Chr(Ch)))  { Change the character's case }
  156.   else
  157.     GetKeyUpCase := Ch;        { Leave the character alone }
  158. end; { GetKeyUpCase }
  159.  
  160. function GetKeyChar(Legal : CharSet) : Char;
  161. { Reads an ASCII key from the keyboard, only accepting keys in Legal }
  162. var
  163.   Ch : Word;
  164. begin
  165.   repeat
  166.     Ch := GetKeyUpCase;
  167.   until (Ch = ESC) or (Chr(Lo(Ch)) in Legal);
  168.   GetKeyChar := Chr(Lo(Ch));
  169. end; { GetKeyChar }
  170.  
  171. procedure Abort(Message : String);
  172. { Aborts the program with an error message }
  173. begin
  174.   AbortMessage := Message;
  175.   Halt(1);
  176. end; { Abort }
  177.  
  178. procedure Beep;
  179. { Produces a low beep on the speaker }
  180. begin
  181.   Sound(220);
  182.   Delay(300);
  183.   NoSound;
  184. end; { Beep }
  185.  
  186. function FileExists(F : String) : Boolean;
  187. { Checks to see if a selected file exists }
  188. var
  189.   SR : SearchRec;
  190. begin
  191.   FindFirst(F, AnyFile, SR);
  192.   FileExists := DosError = 0;
  193. end; { FileExists }
  194.  
  195. function Min(N1, N2 : Longint) : Longint;
  196. { Returns the smaller of two numbers }
  197. begin
  198.   if N1 <= N2 then
  199.     Min := N1
  200.   else
  201.     Min := N2;
  202. end; { Min }
  203.  
  204. function Max(N1, N2 : Longint) : Longint;
  205. { Returns the larger of two numbers }
  206. begin
  207.   if N1 >= N2 then
  208.     Max := N1
  209.   else
  210.     Max := N2;
  211. end; { Max }
  212.  
  213. function NumToString(N : Longint) : String;
  214. { Converts a number to a string }
  215. var
  216.   S : String[80];
  217. begin
  218.   Str(N, S);
  219.   NumToString := S;
  220. end; { NumToString }
  221.  
  222. function UpperCase(S : String) : String;
  223. { Returns an all-upper case version of a string }
  224. var
  225.   Counter : Word;
  226. begin
  227.   for Counter := 1 to Length(S) do
  228.     S[Counter] := UpCase(S[Counter]);
  229.   UpperCase := S;
  230. end; { UpperCase }
  231.  
  232. function FillString(Len : Byte; Ch : Char) : String;
  233. var
  234.   S : String;
  235. begin
  236.   S[0] := Chr(Len);
  237.   FillChar(S[1], Len, Ch);
  238.   FillString := S;
  239. end; { FillString }
  240.  
  241. function TruncStr(TString : String; Len : Byte) : String;
  242. { Truncates a string to a specified length }
  243. begin
  244.   if Length(TString) > Len then
  245.     Delete(TString, Succ(Len), Length(TString) - Len);
  246.   TruncStr := TString;
  247. end; { TruncStr }
  248.  
  249. function PadChar(PString : String; Ch : Char; Len : Byte) : String;
  250. { Pads a string to a specified length with a specified character }
  251. var
  252.   CurrLen : Byte;
  253. begin
  254.   CurrLen := Min(Length(PString), Len);
  255.   PString[0] := Chr(Len);
  256.   FillChar(PString[Succ(CurrLen)], Len - CurrLen, Ch);
  257.   PadChar := PString;
  258. end; { PadChar }
  259.  
  260. function CenterStr(S : String; Width : Byte) : String;
  261. { Center a string within a certain width }
  262. begin
  263.   if not (Length(S) > Width) then
  264.      S := LeftJustStr(LeftJustStr('', (Width - Length(S)) shr 1) + S, Width);
  265.   CenterStr := S;
  266. end; { CenterStr }
  267.  
  268. function LeftJustStr(S : String; Width : Byte) : String;
  269. { Left-justify a string within a certain width }
  270. begin
  271.   LeftJustStr := PadChar(S, ' ', Width);
  272. end; { LeftJustStr }
  273.  
  274. function RightJustStr(S : String; Width : Byte) : String;
  275. { Right-justify a string within a certain width }
  276. begin
  277.   S := TruncStr(S, Width);
  278.   RightJustStr := LeftJustStr('', Width - Length(S)) + S;
  279. end; { RightJustStr }
  280.  
  281. function ColToString(Col : Word) : String;
  282. { Converts a column to a string }
  283. var
  284.   S : String[4];
  285.   W : Word;
  286. begin
  287.   if Col > 18278 then                     { Column is 4 letters }
  288.     S := Chr(Ord('A') + ((Col - 18279) div 17576)) 
  289.   else
  290.     S := '';
  291.   if Col > 702 then                { Column is at least 3 letters }
  292.     S := S + Chr(Ord('A') + (((Col - 703) mod 17576) div 676));
  293.   if Col > 26 then                 { Column is at least 2 letters }
  294.     S := S + Chr(Ord('A') + (((Col - 27) mod 676) div 26));
  295.   S := S + Chr(Ord('A') + (Max(Pred(Col),0) mod 26));
  296.   ColToString := S;
  297. end; { ColToString }
  298.  
  299. function RowToString(Row : Word) : String;
  300. { Converts a row to a string }
  301. begin
  302.   RowToString := NumToString(Row);
  303. end; { RowToString }
  304.  
  305. function StringToCol(S : String; MaxCols : Word) : Word;
  306. { Converts a string to a column }
  307. var
  308.   L : Byte;
  309.   C : Longint;
  310. begin
  311.   StringToCol := 0;      { Return 0 by default to indicate a bad column }
  312.   L := Length(S);
  313.   if L = 0 then
  314.     Exit;
  315.   S := UpperCase(S);
  316.   for C := 1 to L do
  317.   begin
  318.     if not (S[C] in Letters) then   { Bad letter - return }
  319.       Exit;
  320.   end;
  321.   C := Ord(S[L]) - Ord(Pred('A'));
  322.   if L > 1 then
  323.     Inc(C, (Ord(S[Pred(L)]) - Ord(Pred('A'))) * 26);
  324.   if L > 2 then
  325.     Inc(C, (Ord(S[L - 2]) - Ord(Pred('A'))) * 676);
  326.   if L > 3 then
  327.     Inc(C, Longint(Ord(S[L - 3]) - Ord(Pred('A'))) * 17576);
  328.   if C > MaxCols then
  329.     Exit;
  330.   StringToCol := C;         { Successful - return column string }
  331. end; { StringToCol }
  332.  
  333. function StringToRow(S : String; MaxRows : Word) : Word;
  334. { Converts a string to a Rown }
  335. var
  336.   R : Longint;
  337.   Error : Integer;
  338. begin
  339.   StringToRow := 0;   { Return 0 by default to indicate a bad row }
  340.   if S = '' then
  341.     Exit;
  342.   Val(S, R, Error);
  343.   if (Error = 0) and (R <= MaxRows) then
  344.     StringToRow := R;
  345. end; { StringToRow }
  346.  
  347. procedure ClearInputBuffer;
  348. { Clears the keyboard buffer }
  349. var
  350.   Ch : Char;
  351. begin
  352.   while KeyPressed do
  353.     Ch := ReadKey;
  354. end; { ClearInputBuffer }
  355.  
  356. {$F+}
  357.  
  358. function UtilHeapError(Size : Word) : Integer;
  359. { Simple heap error handler - returns a nil pointer if allocation was not
  360.   successful }
  361. begin
  362.   UtilHeapError := 1;
  363. end; { UtilHeapError }
  364.  
  365. procedure UtilExit;
  366. { Called on exit to print abort message and restore exit procedure }
  367. begin
  368.   if AbortMessage <> '' then
  369.     Writeln(AbortMessage + '.');
  370.   ExitProc := SavedExitProc;
  371. end; { UtilExit }
  372.  
  373. {$F-}
  374.  
  375. begin
  376.   SavedExitProc := ExitProc;
  377.   HeapError := @UtilHeapError;
  378.   ExitProc := @UtilExit;
  379. end.
  380.